package com.ctshk.rpc.finance.mapper;

import com.ctshk.rpc.finance.base.CustomBaseMapper;
import com.ctshk.rpc.finance.dto.supplier.ReconciliationDTO;
import com.ctshk.rpc.finance.dto.supplier.ReconciliationDetailDTO;
import com.ctshk.rpc.finance.entity.FinanceSupplierReconciliationDetail;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.util.List;

/**
 * <p>
 * 财务供应商对账单明细 Mapper 接口
 * </p>
 *
 * @author 叶冠峰
 * @since 2021-04-08
 */
public interface FinanceSupplierReconciliationDetailMapper extends CustomBaseMapper<FinanceSupplierReconciliationDetail> {

    /**
     * 查询应付金额列表
     * @param id
     * @return
     */
    @Select("SELECT order_type,payable_amount,currency_name \n" +
            "FROM t_finance_supplier_reconciliation_detail \n" +
            "WHERE reconciliation_id = #{reconciliation_id} AND is_deleted = 0")
    List<ReconciliationDetailDTO> queryDetailByReconciliationReconciliation(@Param("reconciliation_id") Long id);

    /**
     * 清空认款明细
     *
     * @param reconciliationId
     * @param id
     * @return
     */
    @Update("UPDATE t_finance_supplier_reconciliation_detail\n" +
            "SET is_deleted = 1,modified_id = #{user_id}\n" +
            "WHERE reconciliation_id = #{reconciliation_id} AND is_deleted = 0")
    Integer cleanByReconciliation(@Param("reconciliation_id")Long reconciliationId, @Param("user_id") Long id);

    /**
     * 查询指定对账单对应供应商预订单ID列表
     * @param reconciliationIdList
     * @return
     */
    @Select("<script>SELECT a.order_id \n" +
            "FROM t_finance_supplier_reconciliation_detail a\n" +
            "LEFT JOIN t_finance_supplier_reconciliation b ON a.reconciliation_id = b.id\n" +
            "WHERE a.reconciliation_id IN " +
            "<foreach collection=\"list\" item=\"id\" index=\"index\" open=\"(\" close=\")\" separator=\",\">\n" +
            "#{id}\n" +
            "</foreach>"+
            "AND b.is_deleted = 0 AND b.pay_status != 1</script>")
    List<Long> queryOrderIdListByReconciliation(@Param("list") List<Long> reconciliationIdList);

    /**
     * 删除对账单明细
     * @param reconciliationId
     * @param userId
     * @return
     */
    @Update("UPDATE t_finance_supplier_reconciliation_detail\n" +
            "SET is_deleted = 1,modified_id = #{user_id}\n" +
            "WHERE reconciliation_id = #{reconciliation_id} AND is_deleted = 0")
    Integer delByReconciliation(@Param("reconciliation_id") Long reconciliationId, @Param("user_id") Long userId);

    /**
     * 根据供应商订单ID查询对账单号
     * @param orderId
     * @return
     */
    @Select("SELECT b.reconciliation_number \n" +
            "FROM t_finance_supplier_reconciliation_detail a\n" +
            "LEFT JOIN t_finance_supplier_reconciliation b ON a.reconciliation_id = b.id\n" +
            "WHERE a.order_id = #{order_id} AND a.is_deleted = 0 LIMIT 1")
    String queryByOrderId(@Param("order_id") Long orderId);

    /**
     * 查询供应商对账单
     * @param id
     * @return
     */
    @Select("SELECT a.reconciliation_id,b.reconciliation_number \n" +
            "FROM t_finance_supplier_reconciliation_detail a\n" +
            "LEFT JOIN t_finance_supplier_reconciliation b ON a.reconciliation_id = b.id\n" +
            "WHERE a.order_id = #{order_id} AND a.is_deleted = 0 LIMIT 1")
    ReconciliationDTO queryBySupplierOrder(@Param("order_id")Long id);
}
